export const Common = {
    isInArr(item, array) {
        for (let i = 0; i < array.length; i++) {
            if (item === array[i]) {
                return true;
            }
        }
        return false;
    },

    setGray(node) {
        if (!cc.isValid(node)) {
            return;
        }
        let sprite = node.getComponent(cc.Sprite);
        if (!sprite) {
            return;
        }
        sprite.setMaterial(0, cc.Material.getInstantiatedBuiltinMaterial('2d-gray-sprite'));
    },

    setNormal(node) {
        if (!cc.isValid(node)) {
            return;
        }
        let sprite = node.getComponent(cc.Sprite);
        if (!sprite) {
            return;
        }
        sprite.setMaterial(0, cc.Material.getInstantiatedBuiltinMaterial('2d-sprite'));
    },

    setGrayRecursively(node) {
        if (!cc.isValid(node)) {
            return;
        }
        this.setGray(node);
        if (node.children.length <= 0) {
            return;
        }
        for (let i = 0; i < node.children.length; i++) {
            this.setGrayRecursively(node.children[i]);
        }
    },
    setNormalRecursively(node) {
        if (!cc.isValid(node)) {
            return;
        }
        this.setNormal(node);
        if (node.children.length <= 0) {
            return;
        }
        for (let i = 0; i < node.children.length; i++) {
            this.setNormalRecursively(node.children[i]);
        }
    },

    isToday(str) {
        if (new Date(str).toDateString() === new Date().toDateString()) {
            //今天
            console.log("当天");
            return true;
        } else if (new Date(str) < new Date()) {
            //之前
            console.log("以前的日期");
            return false;
        }
    },

    loadAvatar(sprite, url, cb) {
        if (!cc.isValid(sprite)) {
            cb && cb(false);
            return;
        }

        cc.loader.load({ url: url, type: 'png' }, (err, res) => {
            if (err) {
                console.log(err);
                cb && cb(false);
                return;
            }
            if (!cc.isValid(sprite)) {
                cb && cb(false);
                return;
            }
            sprite.spriteFrame = new cc.SpriteFrame(res);
            cb && cb(true);
        })
    },

    setSpriteFrame(sprite, url, cb) {
        if (!cc.isValid(sprite)) {
            cb && cb(false);
            return;
        }
        cc.loader.loadRes(url, cc.SpriteFrame, (err, res) => {
            if (err) {
                console.log(err);
                cb && cb(false);
                return;
            }
            if (!cc.isValid(sprite)) {
                cb && cb(false);
                return;
            }
            sprite.spriteFrame = res;
            cb && cb(true);
        });
    },

    shakeEffect (node, duration) {
        let oldPos = node.position;
        node.runAction(
            cc.repeatForever(
                cc.sequence(
                    cc.moveTo(0.02, oldPos.add(cc.v2(5, 7))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(-6, 7))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(-13, 3))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(3, -6))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(-5, 5))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(2, -8))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(-8, -10))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(3, 10))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(0, 0)))
                )
            )
        );

        setTimeout(() => {
            node.stopAllActions();
            node.position = oldPos;
        }, duration * 1000);
    },

}